package pl.spikes.xmpp;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smackx.muc.HostedRoom;
import org.jivesoftware.smackx.muc.InvitationListener;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.muc.RoomInfo;

import pl.spikes.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class XMPPJoinGameActivity extends Activity implements OnClickListener {
	TableLayout game_list;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.xmpp_join_a_game);
		
		try {
			Collection<HostedRoom> hosted_games = getConferenceRoom();
			game_list = (TableLayout)findViewById(R.id.game_list);
			int counter = 1;
			for (HostedRoom hg: hosted_games) {
				TableRow game_row = new TableRow(this);
				game_row.setId(counter);
				TextView game_JID = new TextView(this);
				game_JID.setText(hg.getJid());
				TextView game_name = new TextView(this);
				game_name.setText(hg.getName());
				game_row.addView(game_JID);
				game_row.addView(game_name);
				game_list.addView(game_row);
				game_row.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						MultiUserChat muc = new MultiUserChat(XMPPLogic.getInstance().getConnection(), "trongame@conference.vps01.fit3140hack.org");
						try {
							muc.join("test player");
							Player player = new Player();
							StringWriter s = new StringWriter();
							player.playerName = muc.getNickname();
							player.commond = "turn left";
							ObjectMapper mapper = new ObjectMapper();
							mapper.writeValue(s, player);
							muc.sendMessage(s.toString());
						} catch (XMPPException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (JsonGenerationException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (JsonMappingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
					}
				});
				
			}
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


	
	public static Collection<HostedRoom> getConferenceRoom() throws XMPPException {
		ConfigureProviderManager.configureProviderManager();
		Collection<HostedRoom> hr = MultiUserChat.getHostedRooms(XMPPLogic.getInstance().getConnection(), "conference.vps01.fit3140hack.org");
		if(!hr.isEmpty()) {
			for(HostedRoom room: hr) {
				Log.i("room name", room.getName());
				Log.i("room JID", room.getJid());
			}
		}
		return hr;
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
